package com.hup.minicontroller.client.service;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.hup.minicontroller.client.ClientApplication;
import com.hup.minicontroller.client.model.AppState;
import com.hup.minicontroller.client.mvp.IMainView;
import com.hup.minicontroller.common.bluetooth.annotation.BtDataHandlerMethod;
import com.hup.minicontroller.common.bluetooth.handler.BtDataHandler;
import com.hup.minicontroller.common.model.dto.ControlConfigDto;
import com.hup.minicontroller.common.model.dto.ScreenDataResponse;
import com.hup.minicontroller.common.netty.annotation.DataHandlerMethod;
import com.hup.minicontroller.common.netty.handler.DataHandler;

/**
 * 服务端消息处理类,相当于Controller
 *
 * @author hugan
 * @date 2021/5/18
 */
public class DataHandlerService implements DataHandler, BtDataHandler {

    private final AppState appState;

    public DataHandlerService() {
        appState = ClientApplication.getBean(MainService.class).getAppState();
    }

    @DataHandlerMethod
    @BtDataHandlerMethod
    public void controlConfigDto(ControlConfigDto controlConfig) {
        appState.getControlConfig().apply(controlConfig);
        ClientApplication.executeIfExist(IMainView.class, IMainView::onControlConfigChanged);
    }

    @DataHandlerMethod
    @BtDataHandlerMethod
    public void screenDataDto(ScreenDataResponse screenDataResponse) {
        byte[] data = screenDataResponse.getData();
        Bitmap screenData = BitmapFactory.decodeByteArray(data, 0, data.length);
        ClientApplication.executeIfExist(IMainView.class, i -> i.onScreenData(screenData));
    }
}
